package br.com.cast.scc.pages.veiculo;



import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enums.StatusRegistroAtivo;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.model.Veiculo;
import br.com.cast.scc.pages.filtros.FiltroPesquisaVeiculos;
import br.com.cast.scc.service.TipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class ConsultarVeiculoIndexForm extends Form<FiltroPesquisaVeiculos> {

	/**
	 * @author Thiago
	 */
	private static final long serialVersionUID = 1L;
	
	@SpringBean(name="veiculoServiceImpl")
	private VeiculoService veiculoService;
	
	@SpringBean(name="tipoVeiculoServiceImpl")
	private TipoVeiculoService tipoVeiculoService;

	private VeiculoGridPanel gridPanel;
	private WebMarkupContainer panelCampoFluvial;
	private WebMarkupContainer panelCampoTerrestre;
	
	
	
	public ConsultarVeiculoIndexForm(String id) {
		super(id);
		
		criaNovo();
	
		
		criaCampos();
		
		FiltroPesquisaVeiculos filtro = new FiltroPesquisaVeiculos(veiculoService);
		setDefaultModel(new CompoundPropertyModel<FiltroPesquisaVeiculos>(filtro));
		
		IChoiceRenderer<DominioTipoVeiculo> renderer = new ChoiceRenderer<DominioTipoVeiculo>("descricao");
		final DropDownChoice<DominioTipoVeiculo> tipoVeiculo = new DropDownChoice<>("tipoVeiculo",tipoVeiculoService.buscarTodos(),renderer);
		
		tipoVeiculo.add(new AjaxFormComponentUpdatingBehavior("change") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				DominioTipoVeiculo tipo = tipoVeiculo.getModelObject();
				
				boolean isTerrestre = tipo.getCodigo().equals(DominioTipoVeiculo.COD_TERRESTRE);
				panelCampoTerrestre.setVisible(isTerrestre);
				panelCampoFluvial.setVisible(!isTerrestre);
				
				target.add(panelCampoFluvial,panelCampoTerrestre);
			}
		});
		
		add(tipoVeiculo);
		
		panelCampoFluvial.add(new TextField<>("nuEmbarcacao"));
		panelCampoTerrestre.add(new TextField<>("placa"));
		panelCampoTerrestre.add(new TextField<>("rntrc"));
		
		IChoiceRenderer<StatusRegistroAtivo> rendererStatus = new ChoiceRenderer<>("descricao");
		add(new RadioChoice<>("status", StatusRegistroAtivo.valores(), rendererStatus));
		
		gridPanel = new VeiculoGridPanel("panelVeiculos", filtro);
		add(gridPanel);
		
		add(new AjaxButton("pesquisar") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				target.add(gridPanel);
			}
		});
		
		
		add(panelCampoFluvial,panelCampoTerrestre);
	}


	private void criaCampos() {
		panelCampoFluvial = new WebMarkupContainer("camposFluviais");
		panelCampoFluvial.setVisible(false);
		panelCampoFluvial.setOutputMarkupPlaceholderTag(true);
		
		panelCampoTerrestre = new WebMarkupContainer("camposTerrestres");
		panelCampoTerrestre.setVisible(false);
		panelCampoTerrestre.setOutputMarkupPlaceholderTag(true);
	}


	@SuppressWarnings("rawtypes")
	private void criaNovo() {
		add(new Link("novo"){

		
			private static final long serialVersionUID = -6658552850595198950L;

			@Override
			public void onClick() {
				setResponsePage(CadastrarVeiculoPage.class);
			}
		});
	}
	
	

}
